//
//  RHChoiceWiFiView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/1.
//

#import "RHChoiceWiFiView.h"
#import <NetworkExtension/NetworkExtension.h>

@interface RHChoiceWiFiView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) UIView *contentView;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) UIPickerView *pickerView;

@property (nonatomic ,strong) MASConstraint *bgViewTopConstraint;

@property (nonatomic ,strong) MASConstraint *bgViewBottomConstraint;


@end
@implementation RHChoiceWiFiView

-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

#pragma mark ------ 获取已经连接过的WiFi列表
-(void)getConfigWifiList
{
//调用过这个方法后的才会有记录- (void)applyConfiguration:(NEHotspotConfiguration *)configuration
//completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler
    [[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> * array) {
        for (NSString * str in array) {
            NSLog(@"加入过的WiFi：%@",str);

        }
    }];
    
    [self.tableView reloadData];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self addSubview:touchView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *contentView = kView;
    contentView.layer.cornerRadius = 8;
    contentView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);
       
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setText:@"选择WiFi".rh_localized];
    [_titleLab setTextAlignment:NSTextAlignmentCenter];
    [_titleLab setFont:[UIFont systemFontOfSize:16]];
    [_titleLab setTextColor:FontColor(@"#969799")];
    _titleLab.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:_titleLab];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(49);
        make.top.mas_offset(10);
    }];
    
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    sureBtn.titleLabel.font = kSYSTEMFONT(15);
    [sureBtn setTitleColor:FontColor(@"#969799") forState:UIControlStateNormal];
    [contentView addSubview:sureBtn];
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titleLab);
        make.right.mas_offset(-15);
        make.height.offset(40);
    }];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setSeparatorColor:FontColor(@"#F5F5F5")];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [contentView addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(0);
        make.height.offset(kUI_WINDOW_H*0.45);
    }];
    
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];

    
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
      
        [weakSelf hidden];
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.choiceResultBlock){
            weakSelf.choiceResultBlock(weakSelf.curSsid);
        }
        [weakSelf hidden];
        
    }];
    
    self.tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"No data" detailStr:@""];
}


- (void)hidden{
    kWeakSelf;
    [self.bgViewBottomConstraint uninstall];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);

    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {
      
        [self removeFromSuperview];
       
    }];
}


- (void)showAnimation{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.bgViewTopConstraint uninstall];
            [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                self.bgViewBottomConstraint = make.bottom.mas_offset(0);

            }];
            [UIView animateWithDuration:0.2 animations:^{
                [self setNeedsLayout];
                [self layoutIfNeeded];
                [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
            }];
    });
    [self getConfigWifiList];
}



- (void)hiddenAnimation{
    
    
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.wifiList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *wifiCell = [tableView dequeueReusableCellWithIdentifier:@"wifiCell"];
    wifiCell.imageView.image = [UIImage imageNamed:@"wifi_icon"];
    wifiCell.textLabel.text =self.wifiList[indexPath.row];
    
    if([wifiCell.textLabel.text isEqualToString:self.curSsid]){
        wifiCell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"agree"]];
    }else{
        wifiCell.accessoryView = nil;
    }
    if(wifiCell == nil){
        wifiCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"wifiCell"];
    }
    wifiCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return wifiCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.curSsid = self.wifiList[indexPath.row];
    [self.tableView reloadData];
//    [self hidden];
}



-(void)dealloc{
    NSLog(@"---->ChoiceWiFiView dealloc");
}


@end
